package day52.pet.controller;

import day52.basic.util.CodeGenerateUtils;
import day52.org.domain.Shop;
import day52.org.mapper.ShopMapper;
import day52.pet.domain.Pet;
import day52.pet.mapper.PetMapper;
import day52.pet.service.IOrderAdoptService;
import day52.pet.domain.OrderAdopt;
import day52.pet.query.OrderAdoptQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.pet.service.IPetService;
import day52.pet.service.impl.PetServiceImpl;
import day52.user.domain.User;
import day52.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/orderAdopt")
public class OrderAdoptController {
    @Autowired
    public IOrderAdoptService orderAdoptService;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IPetService petService;



    /**
     * 保存和修改公用的
     * @param orderAdopt  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderAdopt orderAdopt){

        if( orderAdopt.getId()!=null){
            orderAdoptService.update(orderAdopt);
        }else{
            orderAdoptService.add(orderAdopt);
        }
        return AjaxResult.me();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        orderAdoptService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderAdopt get(@PathVariable("id")Long id) {

        return orderAdoptService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderAdopt> list(){

        return orderAdoptService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderAdopt> json(@RequestBody OrderAdoptQuery query) {

        return orderAdoptService.pageQuery(query);
    }

    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderAdopt order){
        String orderSn = CodeGenerateUtils.generateOrderSn(10L);
        Pet pet = petMapper.findOne(order.getPetId());
        order.setOrderSn(orderSn);
        order.setState(0);
        order.setPetId(pet.getId());
        order.setUserId(pet.getUserId());
        order.setShopId(pet.getShopId());
        orderAdoptService.add(order);
        ArrayList<Long> list = new ArrayList<>();
        list.add(pet.getId());
        petService.offsale(list);
        return AjaxResult.me();
    }
}
